using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using EmguTool;
using System.Net;
using System.Net.Sockets;

namespace Net5From
{
    public partial class TMainForm : Form
    {

        [DllImport("User32.dll")]
        public extern static System.IntPtr GetDC(System.IntPtr hWnd);
        public TMainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.IntPtr DesktopHandle = GetDC(System.IntPtr.Zero);
            Graphics g = Graphics.FromHdc(DesktopHandle);
            g.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 10, 100, 100));
        }
        /// <summary>
        /// 获取全屏截图
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetWindow()
        {
            Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics imgGraphics = Graphics.FromImage(image);
            imgGraphics.CompositingQuality = CompositingQuality.HighQuality;
            //设置截屏区域
            imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            return image;
        }
        /// <summary>
        /// 获取全屏截图
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetWindow(int width, int height)
        {
            Bitmap image = new Bitmap(width, height);
            Graphics imgGraphics = Graphics.FromImage(image);
            imgGraphics.CompositingQuality = CompositingQuality.HighQuality;
            //设置截屏区域
            imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
            return image;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var img = GetWindow(640, 368);
            img.Save("st.png");
        }

        [DllImport("user32.DLL")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.DLL")]
        public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
        [DllImport("user32.DLL")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent,
        IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
        out uint dwProcessId);

        public const uint PROCESS_VM_OPERATION = 0x0008;
        public const uint PROCESS_VM_READ = 0x0010;
        public const uint PROCESS_VM_WRITE = 0x0020;

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(uint dwDesiredAccess,
        bool bInheritHandle, uint dwProcessId);
        public const uint MEM_COMMIT = 0x1000;
        public const uint MEM_RELEASE = 0x8000;

        public const uint MEM_RESERVE = 0x2000;
        public const uint PAGE_READWRITE = 4;

        [DllImport("kernel32.dll")]
        public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
        uint dwSize, uint flAllocationType, uint flProtect);

        [DllImport("kernel32.dll")]
        public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
        uint dwSize, uint dwFreeType);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr handle);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
        IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

        public const int WM_USER = 0x0400;
        public const int TB_BUTTONCOUNT = WM_USER + 24;
        public const int TB_GETBUTTONTEXTW = WM_USER + 75;

        private void ss()
        {
            IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ReBarWindow32", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "MSTaskSwWClass", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
            int vCount = SendMessage(vHandle, TB_BUTTONCOUNT, 0, 0);
            uint vProcessId;
            GetWindowThreadProcessId(vHandle, out vProcessId);
            IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
            PROCESS_VM_WRITE, false, vProcessId);
            IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 0x1000,
            MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
            char[] vBuffer = new char[256];
            uint vNumberOfBytesRead = 0;
            try
            {
                for (int i = 0; i < vCount; i++)
                {
                    SendMessage(vHandle, TB_GETBUTTONTEXTW, i, vPointer.ToInt32());
                    ReadProcessMemory(vProcess, vPointer,
                    Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
                    vBuffer.Length * sizeof(char), ref vNumberOfBytesRead);
                    int l = 0;
                    for (int j = 0; j < vBuffer.Length; j++)
                    {
                        if (vBuffer[j] == (char)0)
                        {
                            l = j;
                            break;
                        }
                    }
                    Console.WriteLine(new string(vBuffer, 0, l));
                }
            }
            finally
            {
                VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
                CloseHandle(vProcess);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ChangeRETC changeRETC = new ChangeRETC();
            changeRETC.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string tableNum = "20210406";
            tableNum = tableNum.Insert(4, "-");
            tableNum = tableNum.Insert(7, "-");
            button4.Text = tableNum;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();

        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            this.IsMdiContainer = true;
            form2.MdiParent = this;
            form2.Show();
            GC.Collect();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Class1 class1 = new Class1();
            var mat = class1.ProcessImage(new Emgu.CV.Mat(@"C:\Users\Administrator\Pictures\Camera Roll\20210809130421.jpg"));
            mat.Save("123.jpg");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            IPAddress[] IPs = Dns.GetHostAddresses("cloud.juzishu.com.cn");
            for (int i = 0; i < IPs.Length; i++)
            {
                MessageBox.Show(IPs[i].ToString());
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            MessageBox.Show(CheckRemotePort("cloud.juzishu.com.cn", 9000).ToString());
            MessageBox.Show(CheckRemotePort("cloud.juzishu.com.cn", 60022).ToString());
          
        }
        bool IsPrivateNetwork3(string ipv4Address)
        {
            if (IPAddress.TryParse(ipv4Address, out var ip))
            {
                byte[] ipBytes = ip.GetAddressBytes();
                if (ipBytes[0] == 10) return true;
                if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) return true;
                if (ipBytes[0] == 192 && ipBytes[1] == 168) return true;
            }

            return false;
        }
        public bool CheckRemotePort(string ipAddress, int port)
        {
            bool result = false;
            try
            {
                IPAddress[] IPs = Dns.GetHostAddresses(ipAddress);
                if (IPs.Length > 0)
                {
                    IPEndPoint point = new IPEndPoint(IPs[0], port);
                    TcpClient tcp = new TcpClient();
                   //tcp.Connect(point);
                  var t=  tcp.ConnectAsync(ipAddress, port);
                    t.Wait();
                    
                    //Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //sock.Connect(point);
                    result = true;
                }
            }
            catch (SocketException ex)
            {
                //10061 Connection is forcefully rejected.
                if (ex.ErrorCode != 10061)
                {
                    
                }
            }
            return result;
        }
    }
}
